package com.caine.multithread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: CaineZhu
 * @Description: 这种输出顺序是 1 11 21 2 12 22 3 13 23 4 14 24 5 15 25   打印10W+次没有乱序
 * @Date: Created in 17:07 2025/09/03
 * @Modified By:
 */
public class ConditionDemo4 {

    public static void main(String[] args) {

        String[] char1 = {"1", "2", "3", "4", "5"};
        String[] char2 = {"11", "12", "13", "14", "15"};
        String[] char3 = {"21", "22", "23", "24", "25"};
        for (int i = 0; i < 10000; i++) {
            ReentrantLock lock = new ReentrantLock();
            Condition condition1 = lock.newCondition();
            Condition condition2 = lock.newCondition();
            Condition condition3 = lock.newCondition();
            CountDownLatch latch2 = new CountDownLatch(1);
            CountDownLatch latch3 = new CountDownLatch(1);

            Thread t1 = new Thread(() -> {
                lock.lock();
                try {
                    for (String c : char1) {
                        System.out.print(c + " ");
                        if (latch2.getCount() > 0) {
                            latch2.countDown();
                        }
                        condition2.signal();
                        condition1.await();
                    }
                    condition2.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }, "t1");
            t1.start();
            Thread t2 = new Thread(() -> {
                try {
                    latch2.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                lock.lock();
                try {
                    for (String c : char2) {
                        System.out.print(c + " ");
                        if (latch3.getCount() > 0) {
                            latch3.countDown();
                        }
                        condition3.signal();
//                    condition1.await();
                        condition2.await();
                    }
                    condition3.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }, "t2");
            t2.start();
            Thread t3 = new Thread(() -> {
                try {
                    latch3.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                lock.lock();
                try {
                    for (String c : char3) {
                        System.out.print(c + " ");
                        condition1.signal();
                        condition3.await();
                    }
                    condition1.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }, "t3");
            t3.start();
            try {
                t1.join();
                t2.join();
                t3.join();
                System.out.println();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }


        }


    }
}
